//
//  InputItem.swift
//  DSP
//
//  Created by tucici on 2024/5/16.
//

import UIKit
import TCCKit
let InputItemHeight = floor(20.0.k_h)
class InputItem: UICollectionViewCell {
    var title : UILabel!
    var longPressBlock : (()->Void)?
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        borderHexColor = HEX_BLACK
        cornerRadius = 4.k_h
        borderWidth = 1
        
        title = UILabel()
        title.font = font(12)
        title.minimumScaleFactor = 0.5
        title.adjustsFontSizeToFitWidth = true
        title.textAlignment = .center
        
        contentView.addSubview(title)
        title.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(2.k_w)
            make.top.bottom.equalToSuperview()
        }
    }
    
    var model = baseModel(){
        didSet{
            title.text = model.name
            switch (model.enable, model.select){
            case (false, _):
                bgHexColor = HEX_BG_DISABLE
                title.textHexColor = HEX_TEXT_DISABLE
                //title.textHexColor = HEX_BLACK
                borderHexColor = HEX_BORDER_DISABLE
                title.font = font(12, .regular)
            case (true,_):
                bgHexColor = model.select ? HEX_RED : HEX_BG
                title.textHexColor = model.select ? HEX_WHITE : HEX_BLACK
                borderHexColor = model.select ? HEX_RED : HEX_BLACK
                title.font = font(12, model.select ? .bold : .regular)
            }
        }
    }
    
    lazy var longpress : UILongPressGestureRecognizer = {
        let long = UILongPressGestureRecognizer(target: self, action: #selector(actionForlongPress))
        contentView.addGestureRecognizer(long)
        return long
    }()
    
    
    @objc func actionForlongPress(_ sender:UILongPressGestureRecognizer){
        if sender.state == .began{
            longPressBlock?()
            let feedbackGenerator = UIImpactFeedbackGenerator(style: .medium)
            feedbackGenerator.impactOccurred()
        }
    }
    
}
